package org.tea.crawl.biz.common;

/**
 * @author lxh
 */
public class ResponseResult<T> {

    private final T data;

    private final int code;

    private final String msg;

    public final static ResponseResult<Object> SUCCESS =
            new ResponseResult<>(null, 200, "成功");

    public final static ResponseResult<Object> SERVER_FAILED =
            new ResponseResult<>(null, 500, "服务器内部异常");

    public final static ResponseResult<Object> UNKNOWN_FAILED =
            new ResponseResult<>(null, 600, "未知的异常");

    public final static ResponseResult<Object> BIZ_FAILED =
            new ResponseResult<>(null, 700, "业务处理异常");

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(data, 200, "成功");
    }

    private ResponseResult() {
        this.data = null;
        this.code = 600;
        this.msg = "未知的处理结果";
    }

    public ResponseResult(T data, int code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }


    public static final class ResponseResultBuilder<T> {
        private T data;
        private int code;
        private String msg;

        private ResponseResultBuilder() {
        }

        public static <T> ResponseResultBuilder<T> builder() {
            return new ResponseResultBuilder<>();
        }

        public ResponseResultBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public ResponseResultBuilder<T> code(int code) {
            this.code = code;
            return this;
        }

        public ResponseResultBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public ResponseResult<T> build() {
            return new ResponseResult<>(data, code, msg);
        }
    }
}
